package com.journey.org.ui.home.news.list

import android.annotation.SuppressLint
import android.app.Application
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableField
import androidx.databinding.ObservableInt
import androidx.lifecycle.viewModelScope
import com.journey.org.BR
import com.journey.org.R
import com.journey.org.app.HttpManager
import com.journey.org.entity.NeteaseNewsEntity
import com.mvvmhabit.org.base.BaseViewModel
import com.mvvmhabit.org.base.MultiItemViewModel
import com.mvvmhabit.org.binding.command.BindingAction
import com.mvvmhabit.org.binding.command.BindingCommand
import com.mvvmhabit.org.custom.LoadingLayout
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import me.tatarka.bindingcollectionadapter2.ItemBinding

/**
 * @Date: 2020/11/23 9:40
 * @Author:小妖
 * @Description: 新闻列表
 */
class NewsListViewModel(application: Application) : BaseViewModel(application) {

    val requestStateField = ObservableInt()

    val onClickAgainLoadCommand = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            page = 0
            loadNeteaseNewsList(onTypeField.get()!!)
        }
    })

    val onTypeField = ObservableField<String>()

    var page = 0

    val onRefreshCommand = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            page = 0
            loadNeteaseNewsList(onTypeField.get()!!)
        }
    })

    val onLoadMoreCommand = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            page += 20
            loadNeteaseNewsList(onTypeField.get()!!)
        }
    })

    val itemBinding = ItemBinding.of<MultiItemViewModel<NewsListViewModel>>
    { itemBinding, _, item ->
        when (item.getItemType().toString()) {
            "body" -> {
                itemBinding.set(BR.viewModel, R.layout.news_list_item)
            }
            "three" -> {
                itemBinding.set(BR.viewModel, R.layout.news_list_three_item)
            }
        }
    }

    val items = ObservableArrayList<MultiItemViewModel<NewsListViewModel>>()

    @SuppressLint("CheckResult")
    fun loadNeteaseNewsList(type: String) {
        val id: String = if (type == "headline") {
            "T1348647909107"
        } else {
            type
        }
        viewModelScope.launch(Dispatchers.Main) {
            if (page == 0) {
                requestStateField.set(LoadingLayout.NETWORK_LOADING)
            }
            val map = withContext(Dispatchers.IO) {
                HttpManager.getInstance().loadNeteaseNewsList("headline", id, page)
            }
            if (!map.isNullOrEmpty()) {
                val entities: List<NeteaseNewsEntity> = map[id]!!
                entities.forEach { entity ->
                    if (entity.imgextra == null) {
                        addItemBody(entity)
                    } else {
                        addThreeImageBody(entity)
                    }
                }
                requestStateField.set(LoadingLayout.HIDE_LAYOUT)
            } else {
                if (page == 0) {
                    requestStateField.set(LoadingLayout.NODATA)
                }
                cancel()
            }
        }
    }

    private fun addItemBody(entity: NeteaseNewsEntity) {
        val bodyViewModel = NewsListItemViewModel(this, entity)
        bodyViewModel.multiItemType("body")
        items.add(bodyViewModel)
    }

    private fun addThreeImageBody(entity: NeteaseNewsEntity) {
        val threeViewModel =
            NewsListThreeItemViewModel(this, entity)
        threeViewModel.multiItemType("three")
        items.add(threeViewModel)
    }
}